package com.bango.testapp;

import java.util.Hashtable;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import com.bango.blackberry.BangoAgent;

public class TestAppScreen extends MainScreen implements FieldChangeListener {

	private ButtonField btnLog;
	private ButtonField btnPostNow;
	private ButtonField btnError;
	private ButtonField btnPageView;
	private ButtonField btnReset;
	private ButtonField btnClear;
	private ButtonField btnExit;

	private ButtonField btnSearch;
	private ButtonField btnLiveVideoStart;
	private ButtonField btnLiveVideoStop;
	private ButtonField btnOnDemandVideoStart;
	private ButtonField btnOnDemandVideoStop;
	private ButtonField btnIdle;
	private ButtonField btnResume;

	private ButtonField btnViewBatch;

	private LabelField lblTitle;

	private BasicEditField txtEventName;
	private BasicEditField txtEventDetail;
	private BasicEditField txtEventValue;
	private BasicEditField txtParamName1;
	private BasicEditField txtParamValue1;
	private BasicEditField txtParamName2;
	private BasicEditField txtParamValue2;

	private HorizontalFieldManager hManager1;
	private HorizontalFieldManager hManager2;
	private HorizontalFieldManager hManager3;
	private HorizontalFieldManager hManager4;
	private HorizontalFieldManager hManager5;

	public TestAppScreen() {
		// super();
		lblTitle = new LabelField("Hello BlackBerry", LabelField.ELLIPSIS
				| Field.USE_ALL_WIDTH);
		setTitle(lblTitle);

		txtEventName = new BasicEditField("Event Name: ", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtEventName);

		txtEventValue = new BasicEditField("Event Value: ", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtEventValue);

		txtEventDetail = new BasicEditField("Event Detail: ", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtEventDetail);

		txtParamName1 = new BasicEditField("P Name 1: ", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtParamName1);
		txtParamValue1 = new BasicEditField("P Value 1:", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtParamValue1);

		txtParamName2 = new BasicEditField("P Name 2: ", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtParamName2);
		txtParamValue2 = new BasicEditField("P Value 2:", "", 999,
				Field.USE_ALL_WIDTH);
		add(txtParamValue2);

		hManager1 = new HorizontalFieldManager();
		hManager2 = new HorizontalFieldManager();
		hManager3 = new HorizontalFieldManager();
		hManager4 = new HorizontalFieldManager();
		hManager5 = new HorizontalFieldManager();

		btnLiveVideoStart = new ButtonField("LV Start",
				ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
		btnLiveVideoStart.setChangeListener(this);
		hManager1.add(btnLiveVideoStart);

		btnLiveVideoStop = new ButtonField("LV Stop", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnLiveVideoStop.setChangeListener(this);
		hManager1.add(btnLiveVideoStop);

		btnSearch = new ButtonField("Search", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnSearch.setChangeListener(this);
		hManager1.add(btnSearch);

		btnOnDemandVideoStart = new ButtonField("OD Start",
				ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
		btnOnDemandVideoStart.setChangeListener(this);
		hManager2.add(btnOnDemandVideoStart);

		btnOnDemandVideoStop = new ButtonField("OD Stop",
				ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
		btnOnDemandVideoStop.setChangeListener(this);
		hManager2.add(btnOnDemandVideoStop);

		btnLog = new ButtonField("Log", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnLog.setChangeListener(this);
		hManager2.add(btnLog);

		btnIdle = new ButtonField("Idle", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnIdle.setChangeListener(this);
		hManager3.add(btnIdle);

		btnResume = new ButtonField("Resume", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnResume.setChangeListener(this);
		hManager3.add(btnResume);

		btnPageView = new ButtonField("P View", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnPageView.setChangeListener(this);
		hManager3.add(btnPageView);

		btnReset = new ButtonField("Reset", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnReset.setChangeListener(this);
		hManager4.add(btnReset);

		btnClear = new ButtonField("Clear", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnClear.setChangeListener(this);
		hManager4.add(btnClear);

		btnError = new ButtonField("Error", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnError.setChangeListener(this);
		hManager4.add(btnError);

		btnExit = new ButtonField("Exit", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnExit.setChangeListener(this);
		hManager4.add(btnExit);

		btnPostNow = new ButtonField("Post Now", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnPostNow.setChangeListener(this);
		hManager5.add(btnPostNow);

		btnViewBatch = new ButtonField("View Batch", ButtonField.CONSUME_CLICK
				| ButtonField.NEVER_DIRTY);
		btnViewBatch.setChangeListener(this);
		hManager5.add(btnViewBatch);

		add(hManager1);
		add(hManager2);
		add(hManager3);
		add(hManager4);
		add(hManager5);

		Reset();

	}

	private void PostNow() {
		BangoAgent.postEvents();
		lblTitle.setText("Events Posted");
	}

	private int getOverload() {
		/*
		 * 1 - Only Event Name Provided
		 * 2 - Event name and value provided
		 * 3 - Event name, value and detail provided
		 * 4 - Event name, value and event parameters provided
		 * 5 - Event name, value, detail and event parameters provided
		 */

		String eventValue = getEventValue();
		String eventDetail = getEventDetail();
		Hashtable eventParameters = getEventParameters();

		if(eventValue == null){ eventValue = "";}
		if(eventDetail == null){ eventDetail = "";}

		int rtnValue = 1;

		if ((eventValue.length() <= 0)) {
			return rtnValue;
		}

		if ((eventDetail.length() <= 0) && eventParameters.isEmpty()) {
			rtnValue = 2;
			return rtnValue;
		}

		if ((eventDetail.length() > 0) && eventParameters.isEmpty()) {
			return 3;
		}

		if ((eventDetail.length() <= 0) && !eventParameters.isEmpty()) {
			return 4;
		}

		if ((eventDetail.length() > 0) && !eventParameters.isEmpty()) {
			rtnValue = 5;
			return rtnValue;
		}

		return rtnValue;
	}

	private void Log() {
		String eventId = txtEventName.getText().toString();

		switch (getOverload()) {
		case 1:
			BangoAgent.onEvent(eventId);
			break;
		case 2:
			BangoAgent.onEvent(eventId, getEventValue());
			break;
		case 3:
			BangoAgent.onEvent(eventId, getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onEvent(eventId, getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onEvent(eventId, getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}

		lblTitle.setText("Log Made");
	}

	private void OnDemandVideoStart() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStart();
			break;
		case 2:
			BangoAgent.onDemandVideoStart(getEventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStart(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStart(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStart(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onDemandVideoStart");
	}

	private void OnDemandVideoStop() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStop();
			break;
		case 2:
			BangoAgent.onDemandVideoStop(getEventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStop(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStop(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStop(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onDemandVideoStop");
	}

	private void LiveVideoStart() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStart();
			break;
		case 2:
			BangoAgent.onLiveVideoStart(getEventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStart(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStart(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStart(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onLiveVideoStart");
	}

	private void LiveVideoStop() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStop();
			break;
		case 2:
			BangoAgent.onLiveVideoStop(getEventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStop(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStop(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStop(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}

		lblTitle.setText("onLiveVideoStop");
	}

	private void Search() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onSearch();
			break;
		case 2:
			BangoAgent.onSearch(getEventValue());
			break;
		case 3:
			BangoAgent.onSearch(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onSearch(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onSearch(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onSearch");
	}

	private void Idle() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onIdle();
			break;
		case 2:
			BangoAgent.onIdle(getEventValue());
			break;
		case 3:
			BangoAgent.onIdle(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onIdle(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onIdle(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onIdle");
	}

	private void Resume() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onResume();
			break;
		case 2:
			BangoAgent.onResume(getEventValue());
			break;
		case 3:
			BangoAgent.onResume(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onResume(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onResume(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("onResume");
	}

	private void Error() {
		BangoAgent.onError("TestError", "This is my test error", "BB");
		lblTitle.setText("Error Logged");
	}

	private void PageView() {
		switch (getOverload()) {
		case 1:
			BangoAgent.onPageView();
			break;
		case 2:
			BangoAgent.onPageView(getEventValue());
			break;
		case 3:
			BangoAgent.onPageView(getEventValue(), getEventDetail());
			break;
		case 4:
			BangoAgent.onPageView(getEventValue(), getEventParameters());
			break;
		default:
			BangoAgent.onPageView(getEventValue(), getEventDetail(),
					getEventParameters());
			break;
		}
		lblTitle.setText("Page View Updated");
	}

	private void Reset() {
		txtEventName.setText("BB Event");
		txtEventDetail.setText("BB Event Detail");
		txtEventValue.setText("BB Event Value");
		txtParamName1.setText("BGO_SOURCE");
		txtParamValue1.setText("BB SOURCE");
		txtParamName2.setText("BGO_TERM");
		txtParamValue2.setText("BB TERM");
	}

	private void Clear() {
		txtEventName.setText("");
		txtEventDetail.setText("");
		txtEventValue.setText("");
		txtParamName1.setText("");
		txtParamValue1.setText("");
		txtParamName2.setText("");
		txtParamValue2.setText("");
	}

	private String getEventValue() {
		return txtEventValue.getText().toString().trim();
	}

	private String getEventDetail() {
		return txtEventDetail.getText().toString().trim();
	}

	private Hashtable getEventParameters() {
		Hashtable eventParameters = new Hashtable();
		if (txtParamName1.getText().toString().length() > 0
				|| txtParamName2.getText().toString().length() > 0) {
			if (txtParamName1.getText().toString().length() > 0) {
				eventParameters.put(txtParamName1.getText().toString(),
						txtParamValue1.getText().toString());
			}

			if (txtParamName2.getText().toString().length() > 0) {
				eventParameters.put(txtParamName2.getText().toString(),
						txtParamValue2.getText().toString());
			}
		}
		return eventParameters;
	}

	private void ViewBatch() {
		System.out.println("\n---Start Batch---\n");
		System.out.println(BangoAgent.viewBatch());
		System.out.println("\n---End Batch---");
	}

	public TestAppScreen(long arg0) {
		super(arg0);
	}

	public boolean onClose() {
		BangoAgent.onEndSession();
		System.exit(0);
		return true;

	}

	public void fieldChanged(Field field, int context) {
		if (field.equals(btnLog)) {
			Log();
		}

		if (field.equals(btnError)) {
			Error();
		}

		if (field.equals(btnPageView)) {
			PageView();
		}

		if (field.equals(btnReset)) {
			Reset();
		}

		if (field.equals(btnClear)) {
			Clear();
		}

		if (field.equals(btnExit)) {
			BangoAgent.onEndSession();
			this.close();
		}

		if (field.equals(btnSearch)) {
			Search();
		}

		if (field.equals(btnLiveVideoStart)) {
			LiveVideoStart();
		}

		if (field.equals(btnLiveVideoStop)) {
			LiveVideoStop();
		}

		if (field.equals(btnOnDemandVideoStart)) {
			OnDemandVideoStart();
		}

		if (field.equals(btnOnDemandVideoStop)) {
			OnDemandVideoStop();
		}

		if (field.equals(btnIdle)) {
			Idle();
		}

		if (field.equals(btnResume)) {
			Resume();
		}

		if (field.equals(btnPostNow)) {
			PostNow();
		}

		if (field.equals(btnViewBatch)) {
			ViewBatch();
		}

	}

}
